package org.vaadin.nf4v.core.handler.impl;

import com.vaadin.ui.Component;
import org.vaadin.nf4v.core.handler.PageFactory;
import org.vaadin.nf4v.core.navigation.target.NavigationTarget;

/**
 * Default PageFactory implementation.
 * Uses reflection to instantiate page.
 */
public class DefaultPageFactory implements PageFactory {

    @Override
    public Component createPage(NavigationTarget target) {
        Class<? extends Component> pageClass = target.getPageClass();
        try {
            return pageClass.newInstance();
        } catch (Exception e) {
            throw new RuntimeException("Problem while instantiating page of class " + pageClass + ". Page should have no-arg constructor", e);
        }
    }
}
